﻿<?PHP
require_once 'class.wp_environment.php';
require_once 'src/facebook.php';

class WPFB extends WpEnvironemt{

	private $SECRET = '';
	private $API_KEY = '';
	public $WPFB_IMPORT_BASE_URL = '';
	public $facebook;
	
	/**************** Constructor functions *********/
	public function __construct($WPFB_IMPORT_BASE_URL)
	{
		WpEnvironemt::__construct();
		$this->WPFB_IMPORT_BASE_URL = $WPFB_IMPORT_BASE_URL;
		$this->initAllVariables();
	}
		
    protected function initAllVariables()
	{
		/*
		$this->CONNECT_URL= $CONNECT_URL;
		$this->REDIRECT_URL= $REDIRECT_URL;
		$this->LOGIN_URL = $LOGIN_URL;
		*/
		$this->updateVariable('API_KEY', get_option('wpfb_KEY'));
		$this->updateVariable('SECRET', get_option('wpfb_SECRET'));
		
		if( $this->API_KEY == '' || $this->SECRET=='')
			throw new Exception('Please setup API_KEY and SECRET from "Settings" menu.');
		
		$this->facebook = new Facebook(array(
		  'appId'  => $this->API_KEY,
		  'secret' => $this->SECRET,
		  'cookie' => true
		));
		$this->fetchURL('me');
		// database tables;
		$this->create_ASDFSDGK8792SD32();
	}
	
	public function updateVariable($name, $value)
	{
		$this->$name= $value;
	}
	
	/**************** Extend capability from parent method *********/
	public function fetchURL($url)
	{
		$responseFromFacebook = $this->postURL($url, null);
		if($responseFromFacebook == null)
			throw new Exception("Unable to connect to facebook server, please check server log.");
		
		return $responseFromFacebook;
	}
	public function postURL($url, $dataForFacebook)
	{	
		try {
			if (isset($_GET['code']) && isset($_GET['page'])) {
				try{
					$this->facebook->api('oauth/access_token', array(
						'client_id'     => $this->API_KEY,
						'client_secret' => $this->SECRET,
						'code'          => $_GET['code'],
						'redirect_uri' => get_bloginfo('wpurl') . "/wp-admin/admin.php?page={$_GET['page']}"
					));
				}catch (Exception $e) {
					$redirect_uri = get_bloginfo('wpurl') . "/wp-admin/admin.php?page={$_GET['page']}";
					echo "<script language='javascript' type='text/javascript'> window.location='$redirect_uri'; </script>";
					return;
				}
			}
			
			$user = $this->facebook->getUser();
			$response = null;
			
			if($url[0] == '/')
				$url = 'v2.0'.$url;
			else
				$url = 'v2.0/'.$url;
			
			if( !$user)
			{
				$params = array(
					'scope' => 'user_groups, friends_groups, publish_actions, manage_pages, publish_stream'
				);
				$loginUrl = $this->facebook->getLoginUrl($params);
				echo '<h1>Please ';
				echo "<a href='$loginUrl'>Login with Facebook</a>";
				echo '</h1>';
				
				return;
			}
				
			$response = $dataForFacebook == null ? $this->facebook->api($url) : $this->facebook->api($url, 'post', $dataForFacebook);
			if( count($response) == 0)
				throw new Exception('Could not fetch any data');

			return $response;
			
		}catch (FacebookApiException $e) {			
			echo "<h1>Error found</h1>";
			$result = $e->getResult();
			$this->printArray($result);
			$user = null;
			echo "<strong>URL used:</strong><pre>$url</pre>";
			throw $e;
		}
	}
}
?>